﻿// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.Core</Project>
// <FileName>PhoneNumberAuthenticationSource.cs</FileName>
// <CreateTime>2017-04-29 23:46</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-29 23:46" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Threading.Tasks;
using Abp.Authorization.Users;
using Abp.Dependency;
using Vecen.BangChuang.BangChuang.Common;
using Vecen.BangChuang.BangChuang.DomainServices.Common;
using Vecen.BangChuang.MultiTenancy;
using Vecen.BangChuang.Validation;

namespace Vecen.BangChuang.Authorization.Users {
    public class PhoneNumberAuthenticationSource: IExternalAuthenticationSource<Tenant, User>, ITransientDependency {
        private readonly ValidationCodeDomainService _validationCodeDomainService;

        public PhoneNumberAuthenticationSource(ValidationCodeDomainService validationCodeDomainService) {
            _validationCodeDomainService = validationCodeDomainService;
        }

        public async Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress,
            string plainPassword,
            Tenant tenant) {
            //var user =
            //    await _userManager.Users.FirstOrDefaultAsync(
            //        c => c.TenantId == tenant.Id && c.PhoneNumber == userNameOrEmailAddress);
            if(!ValidationHelper.IsMobilePhone(userNameOrEmailAddress)) {
                return false;
            }
            return await _validationCodeDomainService.ValidationAsync(userNameOrEmailAddress, plainPassword,
                ValidationCodeType.QuickLogin);
        }

        public Task<User> CreateUserAsync(string userNameOrEmailAddress, Tenant tenant) {
            throw new System.NotImplementedException();
        }

        public Task UpdateUserAsync(User user, Tenant tenant) {
            throw new System.NotImplementedException();
        }

        public string Name { get; } = "PhoneNumber";
    }
}